import { request } from "../../request/index.js";
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    info:{
      type:Object,
      value:{}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    
  },
  

  /**
   * 组件的方法列表
   */
  methods: {
    handleDetail(e) {
      // navigate to detail. send info.
      var that = this;
      wx.navigateTo({
        url: '/pages/detail/detail',
        success: function(res) {
          res.eventChannel.emit('info', {info: that.properties.info})
        }
      })
    },
    handleCollect(e) {
      const user_info=wx.getStorageSync("user_info");
      if(user_info){
        const marked = this.properties.info.marked;
        const data = {
          user_id: user_info.id,
          goods_id: this.properties.info.id,
          mark: !marked
        };
        // const res = await request({
        //   url:"/mark",
        //   data: data
        // })
        const status = true;
        const task = marked ? '取消收藏' : '收藏';
        if(status){
          wx.showToast({
            title: task + '成功！'
          })
          let info = this.properties.info;
          info.marked = !marked;
          this.setData({
            info: info
          })
        } else {
          wx.showToast({
            title: task + '失败，请稍后再试！'
          })
        }
      } else {
        wx.showModal({
          title: '您未登录',
          content: '是否跳转到登录界面？',
          success(res){
            if(res.confirm) {
              wx.navigateTo({
                url: '/pages/login/login'
              })
            }
          }
        })
      }
    }
  }
})
